#ifndef _CDMAAPI_H_
#define _CDMAAPI_H_

#include "nano_db.h"
#include "syscore.h"
#include "apis.h"
#include "network_frame.h"

#ifdef __cplusplus
extern "C" {
#endif

#define CDMA_BOARD_IP                   "192.168.178.203"
#define CDMA_BOARD_UDP_PORT             "6668"
#define CDMA_MODULE_IP                  "192.168.178.111"
#define CDMA_MODULE_UDP_PORT            "6667"
 
/**
 *****************************************************************************************
 * Message ID between cdma baseband card and cdma module
 * the same for request and response
 *
 *****************************************************************************************
 */
#define CDMA_BASEBAND_CFGSEARCH         0x01
#define CDMA_BASEBAND_CFGRESET          0X02
#define CDMA_BASEBAND_SET_FREQUENCY     0X05
#define CDMA_BASEBAND_STOP              0X06
#define CDMA_BASEBAND_RSTSYS            0X07
#define CDMA_BASEBAND_FREQUECY_SCAN     0x03
#define CDMA_BASEBAND_SET_NEIGHBOR      0x04    
#define CDMA_BASEBAND_FILE_READ         0x08
#define CDMA_BASEBAND_CARRIER_RESET     0x09


#define CDMA_BASEBAND_SET_TXGAIN        0x23
#define CDMA_BASEBAND_SET_RXGAIN        0x24
//#define CDMA_BASEBAND_HEART_BEAT        0x20
#define CDMA_BASEBAND_COLLECT_UEID      0x21
#define CDMA_BASEBAND_SCAN_RESULT       0x25



/***
 *****************************************************************************************
 * Message ID between cdma baseband card and cdma module
 * the response of request
 *
 *****************************************************************************************
 */

#define CDMA_BASEBAND_RSP_CFGSEARCH         0x11
#define CDMA_BASEBAND_RSP_CFGRESET          0X12
#define CDMA_BASEBAND_RSP_FREQUECY_SCAN     0X13
#define CDMA_BASEBAND_RSP_SET_FREQUENCY     0X15
#define CDMA_BASEBAND_RSP_STOP              0X16
#define CDMA_BASEBAND_RSP_RSTSYS            0X17
#define CDMA_BASEBAND_UPLOAD                0x18
#define CDMA_BASEBAND_RSP_CARRIER_RESET     0x19
#define CDMA_BASEBAND_HEART_BEAT            0x1F
#define CDMA_BASEBAND_HEART_BEAT1           0x3A
/***
 *****************************************************************************************
 * MESSAGE paraType of the baseband
 * 
 *
 *****************************************************************************************
 */

#define OMC_PARAM_SOFTVER	0x0001
#define OMC_PARAM_NTPSRV	0x0003
#define OMC_PARAM_RUNTIME	0x0004
#define OMC_PARAM_DEVTIME	0x0005
#define OMC_PARAM_MCC		0x0101
#define OMC_PARAM_MNC		0x0102
#define OMC_PARAM_LAC		0x0103
#define OMC_PARAM_CI		0x0104
#define OMC_PARAM_CFGMODE	0x010A
#define OMC_PARAM_WORKMODE	0x010B
#define OMC_PARAM_CAPTIME	0x010C

#define OMC_PARAM_FREQ		0x0150
#define OMC_PARAM_DNATT		0x0151
#define OMC_PARAM_UPATT		0x0152

#define OMC_PARAM_900_START_FREQ    0x0156
#define OMC_PARAM_900_END_FREQ      0x0157
#define OMC_PARAM_1800_START_FREQ   0x0158
#define OMC_PARAM_1800_END_FREQ     0x0159
#define OMC_PARAM_FREQ_OFFSET       0x015f

#define OMC_PARAM_CARRIER   0x0200
#define OMC_PARAM_IMSI		0x0211
#define OMC_PARAM_IMEI		0x0212
#define OMC_PARAM_SIGQUA	0x0215

#define OMC_PARAM_ESN	    0x0214

//CDMA PARAM
#define OMC_PARAM_PN		  0x0110
#define OMC_PARAM_SID	      0x0111
#define OMC_PARAM_NID	      0x0112
#define OMC_PARAM_BSID	      0x0113
#define OMC_PARAM_REGNUM	  0x0114
#define OMC_PARAM_MODEL       0x0119
#define OMC_PARAM_SCANTIME    0x011a
#define OMC_PARAM_SCANPERIOD  0x011b

#define OMC_PARAM_WORK_FREQ1	        0x0120
#define OMC_PARAM_WORK_FREQ1_MODE	    0x0121
#define OMC_PARAM_WORK_FREQ1_SCANTIME   0x0122
#define OMC_PARAM_WORK_FREQ1_SCANCAPTIME    0x0123
#define OMC_PARAM_WORK_FREQ1_NEIBOR_CELL1    0x0124
#define OMC_PARAM_WORK_FREQ1_NEIBOR_CELL2    0x0125
#define OMC_PARAM_WORK_FREQ1_NEIBOR_CELL3    0x0126
#define OMC_PARAM_WORK_FREQ1_NEIBOR_CELL4    0x0127

#define OMC_PARAM_WORK_FREQ2	        0x0128
#define OMC_PARAM_WORK_FREQ2_MODE	    0x0129
#define OMC_PARAM_WORK_FREQ2_SCANTIME   0x012a
#define OMC_PARAM_WORK_FREQ2_SCANCAPTIME    0x012b
#define OMC_PARAM_WORK_FREQ2_NEIBOR_CELL1    0x012c
#define OMC_PARAM_WORK_FREQ2_NEIBOR_CELL2    0x012d
#define OMC_PARAM_WORK_FREQ2_NEIBOR_CELL3    0x012e
#define OMC_PARAM_WORK_FREQ2_NEIBOR_CELL4    0x012f

#define OMC_PARAM_WORK_FREQ3	        0x0130
#define OMC_PARAM_WORK_FREQ3_MODE	    0x0131
#define OMC_PARAM_WORK_FREQ3_SCANTIME   0x0132
#define OMC_PARAM_WORK_FREQ3_SCANCAPTIME    0x0133
#define OMC_PARAM_WORK_FREQ3_NEIBOR_CELL1    0x0134
#define OMC_PARAM_WORK_FREQ3_NEIBOR_CELL2    0x0135
#define OMC_PARAM_WORK_FREQ3_NEIBOR_CELL3    0x0136
#define OMC_PARAM_WORK_FREQ3_NEIBOR_CELL4    0x0137

#define OMC_PARAM_WORK_FREQ4	        0x0138
#define OMC_PARAM_WORK_FREQ4_MODE	    0x0139
#define OMC_PARAM_WORK_FREQ4_SCANTIME   0x013a
#define OMC_PARAM_WORK_FREQ4_SCANCAPTIME     0x013b
#define OMC_PARAM_WORK_FREQ4_NEIBOR_CELL1    0x013c
#define OMC_PARAM_WORK_FREQ4_NEIBOR_CELL2    0x013d
#define OMC_PARAM_WORK_FREQ4_NEIBOR_CELL3    0x013e
#define OMC_PARAM_WORK_FREQ4_NEIBOR_CELL4    0x013f

    
#define CDMA_CARRIER_INDEX_0            0x0
#define CDMA_CARRIER_INDEX_1            0x1

#define CDMA_PARITY_MASK                0xFF

#define TICK_INTERVAL                  1000
#define TICK_SECOND                    (TICK_INTERVAL / 1000)    

#define INVALID_CMD_CODE               0xffff
#define INVALID_SRC_ID                 0xffff

#define UE_ID_LENGTH                   16 

#define AUTOBCCHMODE_1                 1 // weakest 
#define AUTOBCCHMODE_2                 2 //
#define AUTOBCCHMODE_3                 3 // 
#define AUTOBCCHMODE_4                 4 //
#define AUTOBCCHMODE_5                 5 // 

#define NUM_OF_NEIGHBOR                100    

typedef enum {
    CDMA_MODULE_IDLE,
    CDMA_MODULE_RESTART,	
    CDMA_MODULE_SHUTDOWN,
    CDMA_MODULE_NSYNC,
	CDMA_MODULE_PRECONFIG,    
	CDMA_MODULE_CONFIGING,
    CDMA_MODULE_CONFIGED,
    CDMA_MODULE_RUNNING,  
} cdmaModuleState_t;

typedef enum {
    CDMA_MODULE_CDMA_BASEBAND_IDLE,
    CDMA_MODULE_CDMA_BASEBAND_TIME_CONFIGED,          //After configuration 
    CDMA_MODULE_CDMA_BASEBAND_CARRIER_CONFIGED,       //After configuration 
    CDMA_MODULE_CDMA_BASEBAND_WAIT_RESPONSE,          //wait for response
    CDMA_MODULE_CDMA_BASEBAND_RESTARTING,             //restarting 
    CDMA_MODULE_CDMA_BASEBAND_SNIFFER_SHUTDOWN,       //shutdown for sniffer
    CDMA_MODULE_CDMA_BASEBAND_SHUTDOWN,               //shutdown
    CDMA_MODULE_CDMA_BASEBAND_RUNNING,                //running    
    CDMA_MODULE_CDMA_BASEBAND_NSYNC                   //CDMA baseband out of sync, no heat beat
} cdmaBasebandState_t;

typedef struct
{
    U32   paramType;
    U32   paramLength;
}paramCode_t;

#define PARAM_CODE_LENGTH sizeof(paramCode_t)
#if 0
typedef struct
{
    U32   frequency;
    U32   mcc;
    U32   mnc;
    U32   lac;
    U32   band;
    U32   carrier;
    U32   neighborsNumToSend; 
    S32   rssiMin;
    U32   thresholdDelta;  
    U32   captime;
    U32   dnatt;
    U32   upatt;
    U32   cnum;
    U32   configMode;
    U32   workMode;  
    U32   startFreq900;
    U32   endFreq900;
    U32   startFreq1800;
    U32   endFreq1800;
    U32   freqOffset; 
}geranParam_t;
#endif
typedef struct
{
    U32    frequency;
    U32    mcc;
    U32    mnc;
    U32    carrier;
    U32    sid;
    U32    nid;
    U32    pn;
    U32    bsid;
    U32    regnum;
    U32    captime;
    U32    dnatt;
    U32    upatt;
    U32    workModel;
    U32    scanTime;
    U32    scanPeriod;
    U32    ResetModel;
    U32    work_freq1;
    U32    work_freq1_mode;
    U32    work_freq1_scantime;
    U32    work_freq1_scancaptime;
    U32    work_freq1_neibor_cell1;
    U32    work_freq1_neibor_cell2;
    U32    work_freq1_neibor_cell3;
    U32    work_freq1_neibor_cell4;
    U32    work_freq2;
    U32    work_freq2_mode;
    U32    work_freq2_scantime;
    U32    work_freq2_scancaptime;
    U32    work_freq2_neibor_cell1;
    U32    work_freq2_neibor_cell2;
    U32    work_freq2_neibor_cell3;
    U32    work_freq2_neibor_cell4;
    U32    work_freq3;
    U32    work_freq3_mode;
    U32    work_freq3_scantime;
    U32    work_freq3_scancaptime;
    U32    work_freq3_neibor_cell1;
    U32    work_freq3_neibor_cell2;
    U32    work_freq3_neibor_cell3;
    U32    work_freq3_neibor_cell4;
    U32    work_freq4;
    U32    work_freq4_mode;
    U32    work_freq4_scantime;
    U32    work_freq4_scancaptime;
    U32    work_freq4_neibor_cell1;
    U32    work_freq4_neibor_cell2;
    U32    work_freq4_neibor_cell3;
    U32    work_freq4_neibor_cell4;
}cdmaParam_t;

typedef struct
{
    U8 autoBccMode;
    U8 isAuto;
    U8 hour;
    U8 minutes;
    U8 second;
    U8 snifferInterval;
    U8 reverved[2];
}snifferParam_t;

typedef struct
{
    U32                     duration;              //duration for not capture the same ue id
    U32                     mode;                //mode 1 for cdma, 2 for cdma, 3 for cdma+cdma
    snifferParam_t          snifferParam;          //parameters for sniffer
    cdmaParam_t             cdmaParam;
    sockaddr_in             remoteAddr;            //cdma baseband ip address and udp port
    sockaddr_in             localAddr;             //local ip address and udp port
}configurationParam_t;

/**
 *****************************************************************************************
 * Message structure between cdma baseband card and cdma module
 *
 *****************************************************************************************
 */
typedef struct
{
    U32   nbCellFreq;   /* 0: for carrier 0 and 1 : for carrier 1 */ 
    U32   nbRssi;       /* rssi for neighboring cell*/
}neighboringCellInfo_t;

/**
 *****************************************************************************************
 * Message structure between cdma baseband card and cdma module
 *
 *****************************************************************************************
 */
typedef struct
{
    U32 length;
    U8  msgIndex    ;      
    U8  msgId       ;   
    U8  carrierIdx  ;   /* 0: for carrier 0 and 1 : for carrier 1 */ 
    U8  rsFiled     ;   

}cdmaBasebandGeneralMsg_t;


#pragma pack(push)
#pragma pack(1)
typedef struct PARM{
    U8 paraLength;
    U16 paraType; 
    U8  paraValue[0];   
}MSGPARAM,*MSGPPARAM;
#pragma pack(pop)

typedef struct
{
     U16           srcID;
     CMDHEADER     cmdHdr;
}opRecord_t;

typedef struct
{
    U16                             selfId;          /* module ID */	
    U16                             hbCounter;       /* count how many heart beat message received */
    U16                             resetCount;
    U16                             timeTick;        /* time Tick for out sync judgement */
    U16                             timeOutThresh;   /* time out threshold */
    U32                             snifferTick;     /* count tick for sniffer */    
    U32                             rspTick[2];      /* rsp tick for waiting response */
    opRecord_t                      record[2];       /* save destination module ID and cmd code */
                                                     /* for response     */
    conn_context                    udpCtxt;         /* udp connection context */
    conn_t                          *udp_conn;        /* udp connection context for cdma module and baseband */
    
    conn_context                    cdma_udpCtxt;         /* cdma_udp connection context */
    conn_t                          *cdma_udp_conn;        /* cdma_udp connection context for cdma module and baseband */
  
    configurationParam_t            config;
    struct ndb_t                    ndb;             /* keep configuration database */
    cdmaModuleState_t                state;           /* cdma module state */
    cdmaBasebandState_t              cdmaPreState[2];  /* cdma baseband current state */
    cdmaBasebandState_t              cdmaCurState[2];  /* cdma baseband previous state */      
    pthread_mutex_t                 mut;             /* for modify cdma baseband state between two thread */
    U8                              msg[256];        /* the lastest udp message */
    U8                              msglength;       /* the lastest udp message length */
    time_t                          curTime;         /* current time counter since from 1970 */
    
    U32                             sendcout[RC_MOD_INVALID];   /* send ok */
    bool                            isSniffering;               /* flag for sniffering */
    U32                             totalNbr;                   /* total neighboring cell */
    U32                             numCmcc;                    /* cmcc neighboring cell */
    U32                             numUnicom;                  /* unicom neighboring cell */
    neighboringCellInfo_t           cmccCell[NUM_OF_NEIGHBOR];
    neighboringCellInfo_t           unicCell[NUM_OF_NEIGHBOR];
    neighboringCellInfo_t           cmccNbrCell[NUM_OF_NEIGHBOR];
    neighboringCellInfo_t           unicNbrCell[NUM_OF_NEIGHBOR];
    neighboringCellInfo_t           sniffCellList[NUM_OF_NEIGHBOR];
}cdmaModuleContext_t;

extern cdmaModuleContext_t  g_CdmaModuleCntx;

#ifdef __cplusplus
}
#endif

#endif
